﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class flt_avail_fares : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write("OK");
        //Response.End();
        //return;
        CRSI.Query instFlt = new CRSI.Query();

        
        StringBuilder buf=new StringBuilder(),fare_key=new StringBuilder();
        int ret_code;
        string sign, ret_msg;
        DataSet dt_fares = new DataSet();

        try
        {
            sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Request.QueryString["airline"] + "|" + Request.QueryString["flt_no"] + "|" + Convert.ToDateTime(Request.QueryString["flt_date"]).ToString("yyyyMMdd") + "|"
                + Request.QueryString["ori"] + "|" + Request.QueryString["des"] +  "|" + Application["HASH_PWD"].ToString(), "MD5");
            dt_fares = instFlt.QueryFltAvailFares(Request.QueryString["airline"], Request.QueryString["flt_no"], Convert.ToDateTime(Request.QueryString["flt_date"]),
                Request.QueryString["ori"], Request.QueryString["des"], 2, Convert.ToInt16(Request.QueryString["seats"]), Convert.ToInt16(Request.QueryString["seg_sn"]), Convert.ToInt16(Request.QueryString["seg_total"]),
                Application["UID"].ToString(), sign, out ret_code, out ret_msg);
            if (ret_code == -1)
            {
                Response.Write(ret_msg);
                ApplicationInstance.CompleteRequest();
                return;
            }
            if (ret_code == 91)
            {
                Response.Write("API unauthorized!");
                ApplicationInstance.CompleteRequest();
                return;
            }
            if (ret_code == 1)
            {
                Response.Write("City timezone not found!");
                ApplicationInstance.CompleteRequest();
                return;
            }
            else if (ret_code == 2)
            {
                Response.Write("Check trip time!");
                ApplicationInstance.CompleteRequest();
                return;
            }
            //Response.Write("Done:"+dt_fares.Rows.Count.ToString());
            //Response.End();
            //return;
            if (dt_fares.Tables[0].Rows.Count == 0)
            {
                Response.Write("<table><tr><td width='50px'></td><td>No available seats or fares.</td></tr></table>");
                ApplicationInstance.CompleteRequest();
                return;
            }
            buf.Clear();
            buf.Append("<table>");
            if (dt_fares.Tables[0].Rows.Count == 1 && Convert.IsDBNull(dt_fares.Tables[0].Rows[0]["fare_code"]))//无票价
            {
                buf.Append("<tr><td>No fare.</td></tr>");
            }
            else
            {
                foreach (DataRow row in dt_fares.Tables[0].Rows)
                {
                    fare_key.Append("AIRLINE=" + Request.QueryString["airline"] + "&FARE_CODE=" + row["fare_code"] + "&DBF=" + row["dbf"].ToString()
                        + "&ORI=" + Request.QueryString["ori"] + "&DES=" + Request.QueryString["des"] + "&SEG_SN=" + row["seg_sn"].ToString()
                        + "&LANG=2&TAX_TYPE=" + Request.QueryString["tax_type"]);
                    buf.Append("<tr><td width='50px'></td><td>" + row["cabin_name"].ToString() + "</td><td>" + row["cabin"].ToString() + row["cabin_avail"].ToString() + "/" + row["class"].ToString().TrimEnd() + row["class_avail"].ToString());
                    if (row["class_status"].ToString() == "R")
                        buf.Append("*");
                    buf.Append("</td><td><input type='radio' name='KEY" + Request.QueryString["seg_sn"] + "' value='" + Request.QueryString["flt_date"]
                        + "|" + Request.QueryString["airline"] + "|" + Request.QueryString["flt_no"] + "|" + Request.QueryString["ori_airport"] + "|" + Request.QueryString["des_airport"] + "|"
                        + row["cabin"] + "|" + row["class"] + "|" + row["fare_code"] + "|" + Request.QueryString["international"] + "|"
                        + Request.QueryString["dep_time"] + "|" + Request.QueryString["arr_time"] + "|" + row["dbf"].ToString() + "|" + row["seg_sn"].ToString());
                    buf.Append("'><a href='fare_details.aspx?" + fare_key.ToString() + "'>");
                    buf.Append(row["fare_name"].ToString() + "</a></input></td><td>" + row["currency"].ToString() + Convert.ToSingle(row["adt_price"]).ToString() + "/" + Convert.ToSingle(row["chd_price"]).ToString() + "/" + Convert.ToSingle(row["inf_price"]).ToString() + "</td><td>+" + row["dbf"].ToString());
                    buf.Append("</td><td>" + row["fare_code"].ToString() + "</td><td>" + row["seg_sn"].ToString() + "/" + row["seg_total"].ToString() + "</td></tr><br/>");
                    fare_key.Clear();
                }
            }
            buf.Append("</table>");
            Response.Write(buf.ToString());
            ApplicationInstance.CompleteRequest();
        }
        catch (Exception ex)
        {
            if (Convert.ToInt16(Application["HTTP_PORT"]) == 1122)
            {
                Response.Write(ex.Message);
                ApplicationInstance.CompleteRequest();
            }
            else
            {
                Response.Write("Duplicated key or null field!");
                ApplicationInstance.CompleteRequest();
            }
            return;
        }

    }
}